﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HPCERP.Infrastructure.DomainBase
{
    public abstract class EntityBase
    {
        private int id;

        /// <summary>
        /// Default Constructor
        /// </summary>
        protected EntityBase()
            : this(-1)
        {
        }


        /// <summary>
        /// Overloaded Constructor
        /// </summary>
        /// <param name="id">An <see cref="System.Int32"/> that 
        /// represents the primary identifier value for the class.</param>
        protected EntityBase(int id)
        {
            this.id = id;
        }

        /// <summary>
        /// An <see cref="System.Object"/> that represents the primary identifier for the class.
        /// </summary>
        public virtual   int Id
        {
            get { return this.id; }
            set { this.id = value; }
        }

        #region Equality Tests
        /// < summary >
        /// Determines whether the specified entity is equal to the
        /// current instance.
        /// < /summary >
        /// < param name=”entity” > An < see cref=”System.Object”/ > that
        /// will be compared to the current instance. < /param >
        /// < returns > True if the passed in entity is equal to the
        /// current instance. < /returns >
        public override bool Equals(object entity)
        {
            if (entity == null || !(entity is EntityBase))
            {
                return false;
            }
            return (this == (EntityBase)entity);
        }

        /// < summary >
        /// Operator overload for determining equality.
        /// < /summary >
        /// < param name=”base1” > The first instance of an
        /// < see cref=”EntityBase”/ > . < /param >
        /// < param name=”base2” > The second instance of an
        /// < see cref=”EntityBase”/ > . < /param >
        /// < returns > True if equal. < /returns >
        public static bool operator ==(EntityBase base1, EntityBase base2)
        {
            if ((object)base1 == null && (object)base2 == null)
            {
                return true;
            }
            if ((object)base1 == null || (object)base2 == null)
            {
                return false;
            }

            if (base1.Id != base2.Id)
            {
                return false;
            }
            return true;
        }

        /// < summary >
        /// Operator overload for determining inequality.
        /// < /summary >
        /// < param name=”base1” > The first instance of an
        /// < see cref=”EntityBase”/ > . < /param >
        /// < param name=”base2” > The second instance of an
        /// < see cref=”EntityBase”/ > . < /param >
        /// < returns > True if not equal. < /returns >
        public static bool operator !=(EntityBase base1, EntityBase base2)
        {
            return (!(base1 == base2));
        }

        /// <summary>
        /// Serves as a hash function for this type
        /// </summary>
        /// <returns>A hsh code for the current Key property.</returns>
        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
        
        #endregion
    }
}
